package com.opo5.logisticsmanagement.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.opo5.logisticsmanagement.common.R;
import com.opo5.logisticsmanagement.dao.MenuDao;
import com.opo5.logisticsmanagement.dao.RoleMenuDao;
import com.opo5.logisticsmanagement.domain.Menu;
import com.opo5.logisticsmanagement.domain.RoleMenu;
import com.opo5.logisticsmanagement.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
* @author lixin
* @description 针对表【tbs_menu】的数据库操作Service实现
* @createDate 2022-07-10 08:55:41
*/
@Service
public class MenuServiceImpl extends ServiceImpl<MenuDao, Menu>
    implements MenuService {

    @Autowired
    private MenuDao menuDao;
    @Autowired
    private RoleMenuDao roleMenuDao;


    @Override
    public R getRoleMenuById(Integer id) {
        QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();
        queryWrapper.isNull("parent_id");
        List<Menu> menus = menuDao.selectList(queryWrapper);
        QueryWrapper<RoleMenu> queryWrapper1 = new QueryWrapper();
        queryWrapper1.eq("role_id",id);
        List<RoleMenu> roleMenus = roleMenuDao.selectList(queryWrapper1);
        List<Integer> checked = new ArrayList<>();
        for (RoleMenu  roleMenu: roleMenus){
            checked.add(roleMenu.getMenuId());
        }
        /*List<Menu> menus = menuDao.selectList(null);
        QueryWrapper<RoleMenu> queryWrapper = new QueryWrapper();
        queryWrapper.select("menu_id").eq("role_id",id);

        List<RoleMenu> roleMenus = roleMenuDao.selectList(queryWrapper);
        List<Integer> list = new ArrayList();
        for (RoleMenu  roleMenu: roleMenus){
            list.add(roleMenu.getMenuId());
        }
        List<RoleMenuVo> temp = new ArrayList<>();
        List<Integer> checked = new ArrayList<>();
        for (Menu menu : menus){
            RoleMenuVo roleMenuVo = new RoleMenuVo();
            if (list.contains(menu.getId())){
                roleMenuVo.setStatus(true);
                checked.add(menu.getId());
            }else{
                roleMenuVo.setStatus(false);
            }
            roleMenuVo.setMenu(menu);
            temp.add(roleMenuVo);
        }*/
        /*List<RoleMenuTreeVo> menu = MenuUtil.getMenu(null, temp);*/
        return R.success(new R().add("menus",menus).add("checked",checked).getMap());
    }

    @Override
    public R getMenu(Integer id) {
        List<Menu> menus = null;
        if(id==null){
            menus = menuDao.selectList(null);
        }else{
            QueryWrapper<RoleMenu> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("role_id",id);
            List<RoleMenu> roleMenus = roleMenuDao.selectList(queryWrapper);
            List<Integer> menuIds = new ArrayList<>();
            for (RoleMenu roleMenu: roleMenus) {
                menuIds.add(roleMenu.getMenuId());
            }
            QueryWrapper<Menu> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.in("id",menuIds);
            menus = menuDao.selectList(queryWrapper1);
        }
        return R.success(menus);
    }

    @Override
    public R addMenu(Menu menu) {
        if (menu.getParentId() != null) {
            Menu menu1 = menuDao.selectById(menu.getParentId());
            if (menu1==null){
                return R.error("没有该父级菜单");
            }
        }
        int flag = menuDao.insert(menu);
        if (flag==1){
            return R.success(null);
        }
        return R.error("添加菜单信息失败");
    }

    @Override
    public R editMenu(Menu menu) {
        int flag = menuDao.updateById(menu);
        if (flag==1){
            return R.success(null);
        }
        return R.error("修改部门信息失败");
    }

    @Override
    @Transactional
    public R delMenu(Integer id) {
        del(id);
        return R.success(null);
    }

    public void del(Integer id){
        QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id",id);
        List<Menu> menus = menuDao.selectList(queryWrapper);
        if (menus.size()!=0){

            menus.forEach(menu -> {
                del((menu.getId()));
            });
        }
        QueryWrapper<RoleMenu> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("menu_id",id);
        roleMenuDao.delete(queryWrapper1);
        menuDao.deleteById(id);
    }
}




